using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;

namespace Wicresoft.IDE.Toolbox
{
    /// <summary>
    /// ToolboxXmlManager - Reads an XML file and populates the toolbox.
    /// </summary>
    public class ToolboxXmlManager
    {
        private Toolbox m_toolbox = null;
        private static readonly string TOOLBOX_CONFIG = GetConfigFile();



        public ToolboxXmlManager(Toolbox toolbox)
        {
            m_toolbox = toolbox;
        }

        private static string GetConfigFile()
        {
            var assemblyLocation = Assembly.GetExecutingAssembly().Location;
            var configPath = new FileInfo(assemblyLocation).Directory.FullName;
            return Path.Combine(configPath, "ToolboxConfig.xml");
        }

        private ToolboxTabCollection InitializeControlsToolTypes()
        {
            ToolboxTabCollection toolboxTabs = new ToolboxTabCollection();
            foreach (GroupType group in ToolboxConfig.Group)
            {
                ToolboxTab toolboxTab = new ToolboxTab();
                toolboxTab.Name = group.Name;

                ToolboxItemCollection toolboxItems = new ToolboxItemCollection();
                foreach (ControlType control in group.Control)
                {
                    Assembly assm = Assembly.Load(control.Assembly.Substring(0, control.Assembly.LastIndexOf(".dll")));
                    Type myType = assm.GetType(control.Type);
                    ToolboxItem toolboxItem = new ToolboxItem();
                    toolboxItem.Type = myType;
                    toolboxItem.Name = control.Name;
                    toolboxItems.Add(toolboxItem);
                }
                toolboxTab.ToolboxItems = toolboxItems;
                toolboxTabs.Add(toolboxTab);
            }
            return toolboxTabs;
        }

        private readonly object lockobj = new object();
        private ToolboxConfig toolConfig;
        public ToolboxConfig ToolboxConfig
        {
            get
            {
                lock (lockobj)
                {
                    if (toolConfig == null)
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(ToolboxConfig));
                        TextReader textReader = new StreamReader(TOOLBOX_CONFIG);
                        toolConfig = (ToolboxConfig)serializer.Deserialize(textReader);
                        textReader.Close();
                        textReader.Dispose();
                    }
                    return toolConfig;
                }
            }
        }

        public ToolboxTabCollection PopulateToolboxInfo()
        {
            if (File.Exists(TOOLBOX_CONFIG))
                return InitializeControlsToolTypes();
            else
                return new ToolboxTabCollection();
        }

        private Toolbox Toolbox
        {
            get
            {
                return m_toolbox;
            }
        }
    }// class
}// namespace
